package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gcgl.domain.YyContract;
import com.ruoyi.gcgl.mapper.YyContractMapper;
import com.ruoyi.gcgl.service.IYyContractService;

/**
 * 项目主合同Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-22
 */
@Service
public class YyContractServiceImpl implements IYyContractService 
{
    @Autowired
    private YyContractMapper sjContractMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询项目主合同
     * 
     * @param id 项目主合同主键
     * @return 项目主合同
     */
    @Override
    public YyContract selectSjContractById(Long id)
    {
        return sjContractMapper.selectSjContractById(id);
    }

    /**
     * 查询项目主合同列表
     * 
     * @param sjContract 项目主合同
     * @return 项目主合同
     */
    @Override
    public List<YyContract> selectSjContractList(YyContract sjContract)
    {
        return sjContractMapper.selectSjContractList(sjContract);
    }

    /**
     * 新增项目主合同
     * 
     * @param sjContract 项目主合同
     * @return 结果
     */
    @Override
    public int insertSjContract(YyContract sjContract){
    	int rows = sjContractMapper.insertSjContract(sjContract);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(sjContract.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", sjContract.getOwner());
        variables.put("level1", sjContract.getYyzxsh());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("yycontract", String.valueOf(sjContract.getId()), variables);
    	
        YyContract sjc = new YyContract();
    	sjc.setId(sjContract.getId());
    	sjc.setProcessInstanceId(pi.getProcessInstanceId());
    	sjContractMapper.updateSjContract(sjc);
    	
        return rows;
    }
    
    @Override
    public int insertSjContract4Save(YyContract sjContract){
    	int rows = sjContractMapper.insertSjContract(sjContract);
        return rows;
    }

    /**
     * 修改项目主合同
     * 
     * @param sjContract 项目主合同
     * @return 结果
     */
    @Override
    public int updateSjContract(YyContract sjContract){
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(sjContract.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", sjContract.getOwner());
//        variables.put("level1", sjContract.getFwsh());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjcontract", String.valueOf(sjContract.getId()), variables);
//    	sjContract.setProcessInstanceId(pi.getProcessInstanceId());
        return sjContractMapper.updateSjContract(sjContract);
    }
    
    @Override
    public int updateSjContract4Save(YyContract sjContract){
        return sjContractMapper.updateSjContract(sjContract);
    }

    /**
     * 批量删除项目主合同
     * 
     * @param ids 需要删除的项目主合同主键
     * @return 结果
     */
    @Override
    public int deleteSjContractByIds(String ids)
    {
        return sjContractMapper.deleteSjContractByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目主合同信息
     * 
     * @param id 项目主合同主键
     * @return 结果
     */
    @Override
    public int deleteSjContractById(Long id)
    {
        return sjContractMapper.deleteSjContractById(id);
    }
}
